import sys
import asyncio
import time
from threading import Thread
from PyQt5.QtCore import Qt, pyqtSignal, QThread ,QTimer 
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem,
    QHeaderView, QMessageBox, QStackedWidget, QComboBox, QFormLayout,
    QDialog, QDialogButtonBox, QStyleFactory, QGraphicsDropShadowEffect,
    QSizePolicy, QButtonGroup  # 新增 QButtonGroup
)

from PyQt5.QtGui import QFont, QIcon, QPixmap, QColor, QDoubleValidator, QIntValidator

class Async_worker(Thread):
    """docstring for Async_worker"""
    def __init__(self, arg):
        super(Async_worker, self).__init__()

    def run(self):
        """线程启动时执行，线程启动后会启动调用run方法，在这里重写run方法"""
        loop = asyncio.new_event_loop()  # 创建独立事件循环
        asyncio.set_event_loop(loop)
        try:
            # 运行协程并获取结果
            result = loop.run_until_complete(self.async_test(1,10))
        except Exception as e:
            print(f"错误: {str(e)}")
        finally:
            loop.close()

    async def async_test(self,delay:int,lp:int):
        ix=0
        while ix<lp:
            await asyncio.sleep(delay)
            print(ix)
            ix+=1




if __name__ == '__main__':

    print(f"start at {time.strftime('%X')}")

    if Thread is not None:
        Thread=None
        print(Thread)
        # str=[]
    app = QApplication(sys.argv)
    window = QWidget()
    window.setWindowTitle('简单窗口示例')
    window.setGeometry(100,100,400,300)
    window.show()   



    async_sub=Async_worker(Thread)
    async_sub.start()

    print(f"end at {time.strftime('%X')}")

    sys.exit(app.exec_())